from PySide2 import QtCore, QtGui, QtWidgets
import platform

class ScrapyWorker(QtCore.QObject):
    logChanged = QtCore.Signal(str)
    started = QtCore.Signal()
    finished = QtCore.Signal(str)

    def __init__(self, parent=None):
        super(ScrapyWorker, self).__init__(parent)
        self._process = QtCore.QProcess(self)
        self._process.setProcessChannelMode(QtCore.QProcess.MergedChannels)
        self._process.readyReadStandardOutput.connect(
            self.on_readyReadStandardOutput)
        self._process.setProgram('scrapy')
        self._process.started.connect(self.started)
        self._process.finished.connect(self.on_finished)
        self.spider = ""

    def on_finished(self):
        self.finished.emit(self.spider)

    def run(self, spider, project=None):
        self.spider = spider
        if project is not None:
            self._process.setWorkingDirectory(project)
        self._process.setArguments(['crawl', spider])
        self._process.start()

    @QtCore.Slot()
    def on_readyReadStandardOutput(self):
        if platform.system() == 'Windows':
            data = self._process.readAllStandardOutput().data().decode('gbk')
        else:
            data = self._process.readAllStandardOutput().data().decode()
        self.logChanged.emit(data)

    @QtCore.Slot()
    def stop(self):
        self._process.kill()

    def spiders(self, project):
        process = QtCore.QProcess()
        process.setProcessChannelMode(QtCore.QProcess.MergedChannels)
        process.setWorkingDirectory(project)
        loop = QtCore.QEventLoop()
        process.finished.connect(loop.quit)
        process.start('scrapy', ['list'])
        loop.exec_()
        return process.readAllStandardOutput().data().decode().split()
